package com.ys.service.sys.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ys.entity.sys.TsOrganizationInfo;
import com.ys.entity.sys.TsUserChange;
import com.ys.entity.sys.TsUserInfo;
import com.ys.mapper.sys.SysMapper;
import com.ys.mapper.sys.TsUserChangeMapper;
import com.ys.service.sys.TsUserChangeService;

@Service("tsUserChangeService")
public class TsUserChangeServiceImpl implements TsUserChangeService {
	@Autowired
	private SysMapper sysMapper;
	@Autowired
	private TsUserChangeMapper tsUserChangeMapper;

	@Override
	public void add(TsUserInfo tsUserInfo, TsUserInfo tsUserInfoOld) {
		
		/*
		 * 新增时、所在岗位变动时、所在组织架构变动时
		 */
		if (tsUserInfo == null
				|| tsUserInfoOld == null
				|| !tsUserInfo.getOrgId().equals(tsUserInfoOld.getOrgId())
				|| tsUserInfo.getRoleId().longValue() != tsUserInfoOld.getRoleId().longValue()) {
			
			TsOrganizationInfo tsOrganizationInfo = new TsOrganizationInfo().setId(tsUserInfo.getOrgId()).queryFirst();
			// 添加用户信息变动
			TsUserChange tsUserChange = null;
			{
				String userId = tsUserInfo.getUserId();
				String userName = tsUserInfo.getUserName();
				String roleName = tsUserInfo.getRoleName();
				Long roleId = tsUserInfo.getRoleId();
				String deptId = tsOrganizationInfo.getOrgid();
				String deptName = tsOrganizationInfo.getDeptName();
				tsUserChange = TsUserChange.tsUserChange(userId, userName, roleId, roleName, deptId, deptName, 1);
				tsUserChangeMapper.insert(tsUserChange);
			}
			
			// 其余数据状态都停用
			sysMapper.updateTsUserChangeExceptCurrentId(tsUserInfo.getUserId(), tsUserChange.getId());
		}
	}

}
